const router = require('koa-router')();
const fs = require('fs');

//过滤
function searchFile() {
    let files = []
    fs.readdirSync(__dirname)
        .forEach(file => {
            if (file.endsWith('.js') && file !== 'index.js') {
                files.push(file)
            }
        })
    return files;
}

//注册路由
function registerRouter(file) {

    file.forEach(fl => {
        let rt = require('./' + fl)
        if (rt) {
            for (const key in rt) {

                switch (rt[key][0]) {
                    case 'get':
                        router.get(key,rt[key][1])
                        break;
                    case 'post':
                        router.post(key,rt[key][1])
                        break;
                    case 'delete':
                        router.delete(key,rt[key][1])
                        break;
                    case 'put':
                        router.put(key,rt[key][1])
                        break;
                    default:
                        break;
                }
            }
        }
    })

}


let files = searchFile();

registerRouter(files);

module.exports = router;







